﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ThreadTest
{
    public class thread
    {
        Mutex mutex = null;
        public thread()
        {
            mutex = new Mutex();
            Run();
        }

        //private void thread1Func()
        //{
        //    for (int count = 0; count < 10; count++)
        //    {
        //        mutex.WaitOne();
        //        TestFunc("Thread1 have run " + count.ToString() + " times");
        //        mutex.ReleaseMutex();
        //    }
        //}
        //private void thread2Func()
        //{
        //    for (int count = 0; count < 10; count++)
        //    {
        //        mutex.WaitOne();
        //        TestFunc("Thread2 have run " + count.ToString() + " times");
        //        mutex.ReleaseMutex();
        //    }
        //}




        private void thread1Func()
        {
            for (int count = 0; count < 10; count++)
            {
                lock (this)
                {
                    mutex.WaitOne();
                    TestFunc("Thread1 have run " + count.ToString() + " times");
                    mutex.ReleaseMutex();
                }
            }
        }
        private void thread2Func()
        {
            for (int count = 0; count < 10; count++)
            {
                lock (this)
                {
                    mutex.WaitOne();
                    TestFunc("Thread2 have run " + count.ToString() + " times");
                    mutex.ReleaseMutex();
                }
            }
        }



        private void TestFunc(string str)
        {
            Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString());
            Thread.Sleep(50);
        }


        public void Run()
        {

            Thread thread1 = new Thread(new ThreadStart(thread1Func));
            Thread thread2 = new Thread(new ThreadStart(thread2Func));

            thread1.Start();
            thread2.Start();

        }


        public void tx()
        {
            EventWaitHandle handleA = new AutoResetEvent(false);
            EventWaitHandle handleB = new AutoResetEvent(false);

            ThreadPool.QueueUserWorkItem(ar =>
            {
                Console.WriteLine("A:我是A,我已经开始运行了");
               
                Thread.Sleep(2000);
                Console.WriteLine("A:我想睡觉了,B你先跑跑吧。"); 
                EventWaitHandle.SignalAndWait(handleB, handleA);
                Console.WriteLine("A:开始工作ing");
                Thread.Sleep(3000);
                Console.WriteLine("A:这个有点难，问下B");
                EventWaitHandle.SignalAndWait(handleB, handleA);
                Console.WriteLine("A:不错，今天任务搞定，我也闪人了。");
            });

            ThreadPool.QueueUserWorkItem(ar =>
            {
                handleB.WaitOne();
                Console.WriteLine("B:我是B,我已经顶替A开始运行了。");
                Thread.Sleep(5000);
                Console.WriteLine("B:我的事情已经做完了，该让A搞搞了，休息一会。");
                EventWaitHandle.SignalAndWait(handleA, handleB);
                Console.WriteLine("B:hi,A我搞定了，下班了。");
                handleA.Set();
            });
        }

    }
}
